package com.example.shop.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.shop.R;
import com.example.shop.activity.ProductDetailActivity;
import com.example.shop.entity.LimitbuyEntity;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

import java.util.ArrayList;
import java.util.List;

public class ItemLvActivityLimitbuyAdapter extends BaseAdapter implements View.OnClickListener {

    private  List<LimitbuyEntity.ProductlistEntity> mEntities;

    private Context context;
    private LayoutInflater layoutInflater;
    private DisplayImageOptions options;
    private List<TextView> mTextViewsLeftTime=new ArrayList<>();
    public ItemLvActivityLimitbuyAdapter(Context context, List<LimitbuyEntity.ProductlistEntity> entities) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
        this.mEntities=entities;

        options = new DisplayImageOptions.Builder()
                .showImageForEmptyUri(R.drawable.product_loading)
                .showImageOnFail(R.drawable.product_loading)
                .resetViewBeforeLoading(true)
                .cacheOnDisk(true)
                .imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .considerExifParams(true)
                .displayer(new FadeInBitmapDisplayer(300))
                .build();
    }
    //设置某一个TextView上的剩余时间
    public void setLimitTime(int timeInMs,int pos){
        for (TextView textView:mTextViewsLeftTime){
            int tvPos= (int) textView.getTag();
            if (tvPos==pos){
                textView.setText("剩余时间： "+getTime(timeInMs));
                break;
            }
        }

    }

    @Override
    public int getCount() {
        return mEntities.size();
    }

    @Override
    public LimitbuyEntity.ProductlistEntity getItem(int position) {
        return mEntities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_lv_activity_limitbuy, parent,false);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((LimitbuyEntity.ProductlistEntity)getItem(position), (ViewHolder) convertView.getTag(),position);
        return convertView;
    }

    private void initializeViews(LimitbuyEntity.ProductlistEntity entity, ViewHolder holder,int pos) {
        //TODO implement
        holder.tvTitleItemLvActivityLimitbuy.setText(entity.getName());
        holder.tvOldPriceItemLvActivityLimitbuy.setText("￥ "+entity.getMarketprice());
        holder.tvNewPriceItemLvActivityLimitbuy.setText("￥ "+entity.getLimitprice());
        holder.tvLefttimeItemLvActivityLimitbuy.setText("剩余时间： "+getTime(Integer.parseInt(entity.getLefttime())));
        holder.tvLefttimeItemLvActivityLimitbuy.setTag(pos);
        holder.btBuyItemLvActivityLimitbuy.setTag(pos);
        holder.btBuyItemLvActivityLimitbuy.setOnClickListener(this);
        ImageLoader.getInstance().displayImage(entity.getPic(),holder.imgvItemLvActivityLimitbuy,options);

    }

    @Override
    public void onClick(View v) {
        int pos= (int) v.getTag();
       LimitbuyEntity.ProductlistEntity entity= mEntities.get(pos);
        Intent intent=new Intent(context, ProductDetailActivity.class);
        intent.putExtra("product",entity);
        context.startActivity(intent);

    }

    protected class ViewHolder {
        private ImageView imgvItemLvActivityLimitbuy;
    private TextView tvTitleItemLvActivityLimitbuy;
    private TextView tvOldPriceItemLvActivityLimitbuy;
    private TextView tvXxtjItemLvActivityLimitbuy;
    private TextView tvNewPriceItemLvActivityLimitbuy;
    private TextView tvLefttimeItemLvActivityLimitbuy;
    private Button btBuyItemLvActivityLimitbuy;
    private ImageView imgvArrow1;

        public ViewHolder(View view) {
            imgvItemLvActivityLimitbuy = (ImageView) view.findViewById(R.id.imgv_item_lv_activity_limitbuy);
            tvTitleItemLvActivityLimitbuy = (TextView) view.findViewById(R.id.tv_title_item_lv_activity_limitbuy);
            tvOldPriceItemLvActivityLimitbuy = (TextView) view.findViewById(R.id.tv_old_price_item_lv_activity_limitbuy);
            tvXxtjItemLvActivityLimitbuy = (TextView) view.findViewById(R.id.tv_xxtj_item_lv_activity_limitbuy);
            tvNewPriceItemLvActivityLimitbuy = (TextView) view.findViewById(R.id.tv_new_price_item_lv_activity_limitbuy);
            tvLefttimeItemLvActivityLimitbuy = (TextView) view.findViewById(R.id.tv_lefttime_item_lv_activity_limitbuy);
            btBuyItemLvActivityLimitbuy = (Button) view.findViewById(R.id.bt_buy_item_lv_activity_limitbuy);
            imgvArrow1 = (ImageView) view.findViewById(R.id.imgv_arrow1);
            mTextViewsLeftTime.add(tvLefttimeItemLvActivityLimitbuy);
        }
    }

    //获取符合我们要求的时间值 ms---->xx时:xx分:xx秒
    public String getTime(int timeInMs){
        int hour= timeInMs/(1000*60*60);
        int leftTime1=timeInMs-hour*(1000*60*60);
        int minute= leftTime1/(1000*60);
        int leftTime2=leftTime1-minute*(1000*60);
        int second= leftTime2/(1000);
        return hour+"时"+minute+"分"+second+"秒";
    }
}
